package edu.henu.personnel.service.user;

import edu.henu.personnel.constant.ErrorConstant;
import edu.henu.personnel.dao.UserDao;
import edu.henu.personnel.entity.User;
import edu.henu.personnel.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @program: PersonnelProject
 * @description: 登录用户接口实现
 * @author: zhuyang
 * @create: 2019-04-26 17:26
 */

@Transactional
@Service
public class UserServiceImpl implements UserService {


    @Autowired
    private UserDao userDao;

    @Override
    public List<User> getAllUser() {
        return userDao.findAllUser();
    }

    @Override
    public int updateUserInfo(User user) {
        if (StringUtils.isBlank(user.getUid())) {
            throw BusinessException.withErrorCode("用户工号不能为空");
        }
        return userDao.updateUserInfo(user);
    }

    @Override
    public int updateUserPwd(User user) {
        return userDao.updateUserPwd(user);
    }

    @Override
    public User getUserInfoById(String uid) {
        return userDao.getUserInfoById(uid);
    }

    @Override
    public int addUser(User user) {
        return userDao.insertUser(user);
    }

    @Override
    public int deleteUser(String uid) {
        return userDao.deleteUserById(uid);
    }

    @Override
    public User login(String uid, String upwd) {
        if (StringUtils.isBlank(uid) || StringUtils.isBlank(upwd)) {
            throw BusinessException.withErrorCode(ErrorConstant.Auth.USERNAME_PASSWORD_IS_EMPTY);
        }
        User user = userDao.getUserInfoByCond(uid, upwd);
//        if (null == user) {
//            throw BusinessException.withErrorCode(ErrorConstant.Auth.USERNAME_PASSWORD_ERROR);
//        }
        return user;
    }
}
